package com.douqu.game.core.entity.ext.data.challenge;


import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.StableDataConfig;
import com.douqu.game.core.config.VipConfig;
import com.douqu.game.core.e.E_StableDataType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.db.TaskDB;
import com.douqu.game.core.entity.ext.TaskInfo;
import com.douqu.game.core.entity.ext.data.BaseData;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.FunctionFactory;
import com.douqu.game.core.factory.TaskFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.CoreUtils;
import com.douqu.game.core.util.LogUtils;

import java.util.List;

/**
 * @author wangzhenfei
 *         2017-11-28 16:46
 *         远征副本
 */
public class ExpeditionData extends BaseData {
    /**
     * 当前用户达到的关卡数量
     */
    private int currentLevelNum;
    /**
     * 最大的关卡数量
     */
    private int maxLevelNum;

    /**
     * 当前轮获得的魔镜晶总数
     */
    private int totalMagicCrystalInLooperNum;

    /**
     * 当前已经刷新的次数
     */
    private int hasFreshBossTimes;

    /**
     * 已经重置的次数
     */
    private int hasResetTimes;

    /**
     * 当天总的重置次数
     */
    private int todayTotalResetTimes;

    /**
     * 已经挑战的次数
     */
    private int hasChallengeTimes;

    /**
     * 当天总的挑战次数
     */
    private int todayTotalChallengeTimes;

    /**
     * 上一个刷新的关卡id
     */
    private int lastLevelId;


    /**
     * 最近一次重置时间
     */
    private long lastOptionTime;


    private Player player;

    public ExpeditionData(Player player) {
        this.player = player;
    }

    @Override
    public void init() {
       VipConfig vipConfig = DataFactory.getInstance().getGameObject(DataFactory.VIP_KEY, player.getVipLevel());
       todayTotalResetTimes = vipConfig.expeditionFreeTimes;
        currentLevelNum = 0;
        maxLevelNum = 0;
        totalMagicCrystalInLooperNum = 0;
        hasFreshBossTimes = 0;
        hasResetTimes = 0;
//        lastLevelId = DataFactory.getInstance().getNextExpeditionBossId(currentLevelNum + 1);
        lastOptionTime = DataFactory.currentTime;

        hasChallengeTimes = 0;
        StableDataConfig stableDataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.EXPEDITION_CHALLENGE_TIMES_BUY.getCode());
        todayTotalChallengeTimes = stableDataConfig.intValue;

        lastOptionTime = DataFactory.currentTime;
    }

    @Override
    public void checkInit() {
        StableDataConfig stableDataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.EXPEDITION_CHALLENGE_TIMES_BUY.getCode());
        if(todayTotalChallengeTimes < stableDataConfig.intValue){
            todayTotalChallengeTimes = stableDataConfig.intValue;
        }

    }

    @Override
    public void reset() {
        VipConfig vipConfig = DataFactory.getInstance().getGameObject(DataFactory.VIP_KEY, player.getVipLevel());
        todayTotalResetTimes = vipConfig.expeditionFreeTimes;
        hasResetTimes = 0;

        hasChallengeTimes = 0;
        StableDataConfig stableDataConfig = DataFactory.getInstance().getGameObject(DataFactory.STABLE_DATA_KEY, E_StableDataType.EXPEDITION_CHALLENGE_TIMES_BUY.getCode());
        todayTotalChallengeTimes = stableDataConfig.intValue;
        lastOptionTime = DataFactory.currentTime;


    }

    @Override
    public void checkReset() {
        if(!CoreUtils.isTodayByOffset(lastOptionTime, DataFactory.currentTime, ConstantFactory.DEFAULT_RESET_FRESH_TIMES_HOUR))
            reset();
        lastOptionTime = DataFactory.currentTime;
    }

    @Override
    public void writeTo(ByteBuffer buffer) {
        buffer.writeInt(currentLevelNum);
        buffer.writeInt(maxLevelNum);
        buffer.writeInt(totalMagicCrystalInLooperNum);
        buffer.writeInt(hasFreshBossTimes);
        buffer.writeInt(hasResetTimes);
        buffer.writeInt(todayTotalResetTimes);
        buffer.writeInt(lastLevelId);
        buffer.writeLong(lastOptionTime);

        //++++++++++++++++++++++++
        buffer.writeInt(hasChallengeTimes);
        buffer.writeInt(todayTotalChallengeTimes);
        buffer.writeBoolean(false);
    }

    @Override
    public void loadFrom(ByteBuffer buffer) {
        currentLevelNum = buffer.readInt();
        maxLevelNum = buffer.readInt();
        totalMagicCrystalInLooperNum = buffer.readInt();
        hasFreshBossTimes = buffer.readInt();
        hasResetTimes = buffer.readInt();
        todayTotalResetTimes = buffer.readInt();
        lastLevelId = buffer.readInt();
        lastOptionTime = buffer.readLong();

        //++++++++++++++++++++++++++++++++++++++
        hasChallengeTimes = buffer.readInt();
        todayTotalChallengeTimes = buffer.readInt();
        buffer.readBoolean();
    }

    /**
     * 获取剩余远征挑战次数
     * @return
     */
    public int getRemainChallengeTime(){
        return todayTotalChallengeTimes - hasChallengeTimes;
    }

    public boolean hasRedPointRemind() {
        //return getHasResetTimes() > 0 || getRemainChallengeTimes() > 0;
        return getRemainChallengeTimes() > 0;
    }

    /**
     * 增加远征总的重置次数
     */
    public void addTodayTotalResetTimes(int times){
        todayTotalResetTimes += times;
    }

    /**
     * 重置挑战
     */
    public void resetChallenge(){
        hasResetTimes++;
        currentLevelNum = 0;
        hasFreshBossTimes = 0;
        totalMagicCrystalInLooperNum = 0;
        lastLevelId = 0;
        lastOptionTime = DataFactory.currentTime;
    }


    /**
     * 挑战成功
     * @param magicCrystal
     */
    public void challengeSuccess(int magicCrystal){
        totalMagicCrystalInLooperNum += magicCrystal;
        currentLevelNum++;
        if(currentLevelNum > maxLevelNum){
            maxLevelNum = currentLevelNum;
        }
        lastLevelId = 0;

    	// 检测任务是否完成
        player.getExtInfo(TaskInfo.class).check(TaskFactory.TASK_TARAGET_EVERYDAY_YUAN_ZHENG, 1);
    }


    /**
     * 召唤下一个boss
     * @return
     */
    public int callBoss(){
        lastLevelId = DataFactory.getInstance().getNextExpeditionBossId(currentLevelNum + 1);
        while (true){
            if(lastLevelId != 0){
                break;
            }
            lastLevelId = DataFactory.getInstance().getNextExpeditionBossId(currentLevelNum + 1);
        }
        LogUtils.debug("召唤了一个BOSS，id为:" + lastLevelId);
        return  lastLevelId;
    }

    /**
     * 购买次数成功
     */
    public void addResetChallengeTimes(int times){
        todayTotalResetTimes++;
    }

    /**
     * 增加换一个BOSS刷新次数
     */
    public int changeBossTimes(){
        hasFreshBossTimes++;
        int i = 10;
        while (i > 0){
            i--;
            int after  = DataFactory.getInstance().getNextExpeditionBossId(currentLevelNum + 1);
            if(after != lastLevelId){
                lastLevelId = after;
                LogUtils.debug("换了一个BOSS，id为:" + lastLevelId);
                break;
            }
        }
        return hasFreshBossTimes;
    }


    /**
     * 获取剩余刷新次数
     */
    public int getRemainResetTimes(){
        if(hasResetTimes > todayTotalResetTimes){
            hasResetTimes = todayTotalResetTimes;
        }
        return todayTotalResetTimes - hasResetTimes;
    }

    /**
     * 获取剩余刷新次数
     */
    public int getRemainChallengeTimes(){
        if(hasChallengeTimes > todayTotalChallengeTimes){
            hasChallengeTimes = todayTotalChallengeTimes;
        }
        return todayTotalChallengeTimes - hasChallengeTimes;
    }



    /**
     *  购买完增加总重置次数
     * @param times
     * @return
     */
    public int addTodayTotalResetTime(int times) {
        if(times > 0){
            todayTotalResetTimes +=   times;
        }
        return todayTotalResetTimes;
    }



    /**
     *  购买完增加总重置次数
     * @param times
     * @return
     */
    public int addTodayTotalChallengeTime(int times) {
        if(times > 0){
            todayTotalChallengeTimes +=   times;
        }
        return todayTotalChallengeTimes;
    }

    /**
     * 挑战次数 +1
     */
    public void addChallengeTimes(long time){
        hasChallengeTimes += 1;
        if(hasChallengeTimes > todayTotalChallengeTimes){
            hasChallengeTimes = todayTotalChallengeTimes;
        }
        lastOptionTime = time;
    }



    public int getCurrentLevelNum() {
        return currentLevelNum;
    }

    public int getMaxLevelNum() {
        return maxLevelNum;
    }

    public int getTotalMagicCrystalInLooperNum() {
        return totalMagicCrystalInLooperNum;
    }

    public int getHasFreshBossTimes() {
        return hasFreshBossTimes;
    }

    public int getHasResetTimes() {
        return hasResetTimes;
    }

    public int getTodayTotalResetTimes() {
        return todayTotalResetTimes;
    }

    public int getLastLevelId() {
        return lastLevelId;
    }

    public int getTodayTotalChallengeTimes() {
        return todayTotalChallengeTimes;
    }

    public void setTotalMagicCrystalInLooperNum(int totalMagicCrystalInLooperNum) {
        this.totalMagicCrystalInLooperNum = totalMagicCrystalInLooperNum;
    }

    public int getHasChallengeTimes() {
        return hasChallengeTimes;
    }

    public void setHasChallengeTimes(int hasChallengeTimes) {
        this.hasChallengeTimes = hasChallengeTimes;
    }

    public void setTodayTotalChallengeTimes(int todayTotalChallengeTimes) {
        this.todayTotalChallengeTimes = todayTotalChallengeTimes;
    }
}
